﻿using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using PdfRotator.Util;
using PdfRotator.ViewModel;

namespace PdfRotator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        internal static readonly MainViewModel MainViewModel = new MainViewModel();

        public MainWindow()
        {
            InitializeComponent();
            DataContext = MainViewModel;
        }

        private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            GridViewColumnFile.Width = Math.Max(0, e.NewSize.Width - GridViewColumnStatus.ActualWidth - 10);
        }

        #region Drag and Drop

        private void Window_DragOver(object sender, DragEventArgs e)
        {
            var paths = e.Data.GetData(DataFormats.FileDrop, true) as string[];
            if (paths == null || !paths.All(PdfUtil.IsPdfFile))
            {
                e.Effects = DragDropEffects.None;
                e.Handled = true;
            }
        }

        private void Window_Drop(object sender, DragEventArgs e)
        {
            var paths = e.Data.GetData(DataFormats.FileDrop, true) as string[];
            if (paths != null)
            {
                var currentPaths = MainViewModel.Files.Select(file => file.Path);
                foreach (var path in paths.Except(currentPaths).Where(PdfUtil.IsValidPdfFile).OrderBy(path => path))
                {
                    MainViewModel.Files.Add(new FileViewModel
                    {
                        Name = Path.GetFileName(path),
                        Path = path,
                        Status = PdfRotationStatus.Waiting,
                    });
                }
            }
        }

        #endregion

        #region Rotate

        private void Execute(Func<string, bool> action)
        {
            IsEnabled = false;

            foreach (var file in MainViewModel.Files)
            {
                file.Status = PdfRotationStatus.Waiting;
            }

            Task.Factory.StartNew(() =>
            {
                foreach (var file in MainViewModel.Files)
                {
                    var ret = action(file.Path);
                    var status = ret ? PdfRotationStatus.Success : PdfRotationStatus.Failure;
                    Dispatcher.Invoke(new Action<FileViewModel>(f => f.Status = status), file);
                }
            }).ContinueWith(t =>
            {
                IsEnabled = true;
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }

        private void ButtonRotate_Click(object sender, RoutedEventArgs e)
        {
            Execute(path => PdfUtil.Rotate(path,
                MainViewModel.OddPageRotation,
                MainViewModel.EvenPageRotation));
        }

        #endregion

    }
}
